<template>
    <div class="add">
    <el-row class="head">
        <el-col >——新增维修备件/维修保障</el-col>
    </el-row>
    <el-row class="title">
        <el-col>新增维修备件</el-col>
    </el-row>
    <el-form  :model="partdata" label-width="100px" :rules="rules" ref="ruleform" class="formrule">
        <el-form-item label="备件名称" prop="name">
            <el-input v-model="partdata.name"></el-input>
        </el-form-item>
        <el-form-item label="数量">
            <el-input-number :min="1" :max="100" v-model="partdata.number" label="备件数量"></el-input-number>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit(partdata)">添加</el-button>
            <el-button @click=reset(partdata)>重置</el-button>
        </el-form-item>
    </el-form>
        </div>
</template>

<script>
    export default {
        name: "add",
        computed:{
            user(){
                return this.$store.getters.user;
            }
        },
        data(){
            return{
                partdata:{
                    id:'',
                    name:'',
                    number:'',
                    partAdd:{
                        partId:'',
                        adminId: '',
                        number:0,
                    }
                },
                rules: {
                    name: [
                        {required: true, message: '请输入添加备件名称', trigger: 'blur'},
                        {min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur'}
                    ],
                }
            }
        },
        methods:{
            onSubmit(partdata){
                this.$refs.ruleform.validate((valid) => {
                    if (valid) {
                        partdata.partAdd.number=partdata.number;
                        partdata.partAdd.adminId=this.user.id;
                        this.$axios.post("/api/v1/admin/part/create",this.partdata).then(result => {

                            return result.data;
                        })
                            .then(data1 => {

                                this.$message({
                                    type: 'success',
                                    message: data1.msg
                                });
                                if (data1.code === 0)
                                {
                                    this.partdata.name='';
                                    this.partdata.number=1;
                                }
                            });
                    } else {
                        this.$message({
                            type: 'error',
                            message: '格式不正确'
                        });
                        return false;
                    }
                });

            },
            reset(partdata)
            {
                partdata.name='';
                partdata.region=0;
            }
        }
    }
</script>

<style scoped>
    .el-input{
        width: 600px;
    }
</style>